package com.yunyi.camunda.listener;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 全局的一个监听
 *
 * @author yunyi
 */
@Component
public class GlobalExecutionListener {

    /**
     * 这个监听监听了所有的create事件
     * 可以通过这个监听实现比方说到达某个节点在指派assignee
     * 这里在隔壁FlowTaskListener对5.bpmn中的审批节点2进行指定节点监听，并改变他的assignee
     *
     * @param delegateTask delegateTask
     */
    @EventListener(condition = "#delegateTask.eventName=='create'")
    public void notify(DelegateTask delegateTask) {
        DelegateExecution execution = delegateTask.getExecution();
        String processInstanceId = execution.getProcessInstanceId();
        System.out.println("processInstanceId: " + processInstanceId);
        String currentActivityId = execution.getCurrentActivityId();
        System.out.println("currentActivityId: " + currentActivityId);
        String currentActivityName = execution.getCurrentActivityName();
        System.out.println("currentActivityName: " + currentActivityName);

        System.out.println("审核流程 - USER TASK - " + delegateTask.getEventName());
        Object assignee = delegateTask.getAssignee();
        System.out.println("审批人：" + assignee);
        Object approve = delegateTask.getVariable("approve");
        System.out.println("审批结果：" + approve);
    }
}
